package applications.camoverlay;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class GalleryActivity extends Activity{
	
	static final int EMPTY_FOLDER=0;
	
	private Resources res;
	ListView gallery;
	
	private AdapterView.OnItemClickListener clickListener = new AdapterView.OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			CameraActivity.iv.setImageBitmap((Bitmap) arg0.getItemAtPosition(arg2));
			CameraActivity.editor.putString("last", ((ImageAdapter)gallery.getAdapter()).getFile(arg2) );
			CameraActivity.editor.commit();
		    finish();
		}
	};
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picturegallery);
        
        res = getResources();
        
        gallery=(ListView)findViewById(R.id.galleryview);
        
        ImageAdapter adapter2;
        try{
        	adapter2 = new ImageAdapter(getApplicationContext());
        	gallery.setAdapter(adapter2);
            gallery.setOnItemClickListener(clickListener);
		} catch(NullPointerException e){
			this.showDialog(EMPTY_FOLDER);
		}
	}
	
	protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        switch(id) {
        	
        case EMPTY_FOLDER:
        	builder.setMessage(res.getString(R.string.emptyfolder))
        	       .setCancelable(false)
        	       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        	           public void onClick(DialogInterface dialog, int id) {
        	                finish();
        	           }
        	       });
        	dialog = builder.create();
    		break;
    		
        default:
            dialog = null;
        }
        return dialog;
    }
}
